package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class MaxConsecutiveOnesIii {
    // 2022-08-24 12:33:04
    public static void main(String[] args) {
        Solution solution = new MaxConsecutiveOnesIii().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int longestOnes(int[] nums, int k) {
            // 0 的个数不超过 k 的最长的窗口  —— 滑动窗口
            // 模板
            int left = 0, right = 0;
            int ans = 0;
            int countZero = 0;
            while (right < nums.length) {
                // 需要重新设置窗口的条件 先统计 0  的个数
                if (nums[right] == 0) countZero++;
                if (countZero > k) {
                    // 移除一个 0
                    if (nums[left] == 0) {
                        countZero--;
                    }
                    left++;
                }
                ans = Math.max(ans, right - left + 1);
                right++;
            }

            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}